%%%-------------------------------------------------------------------
%%% @author admin
%%% @copyright (C) 2021, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 31. 十二月 2021 16:04
%%%-------------------------------------------------------------------
-module(mod_route).
-author("admin").

-include("log.hrl").
-include("account.hrl").
-include("player.hrl").

%% API
-export([
    evt_route_call/3,
    evt_route_cast/3,
    msg_route/2,
    msg_mod/1
]).

evt_route_call(MsgId, Data, Account = #account{player = Player}) ->
    Handler = msg_mod(MsgId),
    case Handler:handle_evt(Data, Player) of
        ok -> {reply, ok, Account};
        {noreply, NPlayer} -> {reply, ok, Account#account{player = NPlayer}};
        {reply, Reply, NPlayer} -> {reply, Reply, Account#account{player = NPlayer}};
        _Err ->
            ?ERR("bad evt return:~w", [_Err]),
            {reply, error, Account}
    end.

evt_route_cast(MsgId, Data, Account) ->
    Handler = msg_mod(MsgId),
    case Handler:handle_evt(Data, Account) of
        ok -> {noreply, Account};
        {noreply, NPlayer} -> {noreply, Account#account{player = NPlayer}};
        _Err ->
            ?ERR("bad evt return:~w", [_Err]),
            {noreply, Account}
    end.

msg_route(Data, Account = #account{account_id = AccId, player = Player}) ->
    MsgId = msg_rec_reflect:msg_id(element(1, Data)),
    Handler = msg_mod(MsgId),
    if
        MsgId div 1000 =< 1 ->
            case Handler:handle_msg(Data, Account) of
                ok ->
                    {noreply, Account};
                {noreply, NAccount} -> {noreply, NAccount};
                {reply, Reply} ->
                    net_lib:send(Reply),
                    {noreply, Account};
                {reply, Reply, NAccount} ->
                    net_lib:send(Reply),
                    {noreply, NAccount};
                {stop, Reason} ->
                    ?ERR("account id ~w stop, reason ~w",[AccId, Reason]),
                    {stop, Reason};
                _Err ->
                    ?ERR("bad msg return:~w", [_Err]),
                    {noreply, Account}
            end;
        true ->
            case Handler:handle_msg(Data, Player) of
                ok ->
                    {noreply, Account};
                {noreply, NPlayer} ->
                    {noreply, Account#account{player = NPlayer}};
                {reply, Reply} ->
                    net_lib:send(Reply),
                    {noreply, Account};
                {reply, Reply, NPlayer} ->
                    net_lib:send(Reply),
                    {noreply, Account#account{player = NPlayer}};
                {stop, Reason} ->
                    ?ERR("account id ~w -> ~w stop, reason ~w",[AccId, Player#player.uuid, Reason]),
                    {stop, Reason};
                _Err ->
                    ?ERR("bad msg return:~w", [_Err]),
                    {noreply, Account}
            end
    end.


msg_mod(MsgId) ->
    case MsgId div 1000 of
        1 -> system_mHandler;
        2 -> account_mHandler;
        _ -> system_mHandler
    end.